<template>
	<view class="page" @touchmove.stop.prevent="moveHandle">
		<view class="top">
			<image src="/static/img/logo.png" class="user"></image>
			<text class="login">登录</text>
			<view class="tel">
				<input type="number" name="phone" id="phone" @input="inputPhone" placeholder="请填写手机号码" />
			</view>
			<view class="checked">
				<input type="number" name="code" id="code" @input="inputCode" placeholder="请填写验证码" />
				<text @click="toCode" v-if="show">获取验证码</text>
				<text v-if="!show">{{count}}秒后重新发送</text>
			</view>
		</view>
		<view class="login-now" @click="toHome">
			<image src="/static/img/login.png"></image>
			<text>立即登录</text>
		</view>
		<text class="register" @click="toWxLogin">去注册</text>
		<view class="bottom-img">
			<image src="/static/img/login-bottom.png"></image>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad: function(options) {
			/* ————————————进来就调用分享方法，右上角点击即可分享————————————————— */
			//分享参数头url
			let url = this.$api.headurl
			this.$hshare.gethShareParam(url+'/#/pages/login/Login')
			/* ————————————进来就调用分享方法，右上角点击即可分享————————————————— */
			
			
			
			
		},
		data() {
			return {
				count: '',
				show: true,
				canBack:false,
				token:'',
				appId:'wx91063cc93c2cffbf',
			}
		},
		methods: {
			moveHandle() {
				return;
			},
			//注册
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/Register'
				})
			},
			//立即登录 登录成功之后跳到首页
			toHome() {
				var memberInfo = uni.getStorageSync("member")
				this.$api.getLogin({
					phone: this.phone,
					verify: this.code,
				}).then(res => {
					if (res.data.status == true) {
						//提示信息
						uni.showToast({
							title: res.data.msg,
						});
						//获取oken 
						uni.setStorage({
						 	key: "token",
							data: res.data.data.token.token,
						})
						if(memberInfo.avatarUrl != res.data.data.token.avatarUrl||memberInfo.nickName != res.data.data.token.nickName){
							//更新用户数据 接口
							this.$api.getUdateInfo({
								phone: this.phone,
								name:memberInfo.nickName,
								headpic:memberInfo.avatarUrl,
								
							}).then(result => {
								if (result.data.status == true) {
									//存 用户缓存 头像 和昵称 不需要openid  默认返回的是 h5的openid
									uni.setStorage({
										key: "member",
										data: res.data.data.token
									})
								}
							})
						}else{
							//存 用户缓存 头像 和昵称
							uni.setStorage({
								key: "member",
								data: res.data.data.token
							})
						}
					
						//登录成功的状态  2
						uni.setStorage({
						 	key: "memberLoginStatus",
							data: 2,
						})
						this.canBack = true
						setTimeout(function(){
							console.log(document.referrer,'---------------------------登录返回上一页')
								//跳转到首页
							
								// window.history.go(-1); 
								//跳转到首页
								var pages = getCurrentPages(); // 当前页面
								var beforePage = pages[pages.length - 2]; // 前一个页面
								console.log(pages,'当前页',beforePage,'前一个页面')
								var _count = 0
								var _length = 0
								for (let i = pages.length-1; i >= 0; i--) {
									if(pages[i].route!="pages/login/Login"&&pages[i].route!="pages/register/Register"){
										console.log(_count,'是否进来','-'._count)
										console.log(window.history.lenth,'长度')
										
										console.log(pages.length,'长度2')
										console.log(_count-pages.length,'最种长度')
										
										var _length =-_count
									}else{
										_count++
										_length--
									}
									
								}
								window.history.go(_length);
								
							
							},1000)

					}
				})

			},
			//获取验证码
			toCode() {
				this.$api.getCode({
					phone: this.phone,
					mark:'login'
				}).then(res => {
					if (res.data.status == true) {
						this.show = false;
						uni.showToast({
							title: res.data.msg
						})
						const time_count = 60;
						if (!this.timer) {
							this.count = time_count;
							this.timer = setInterval(() => {
								if (this.count > 0 && this.count <= time_count) {
									this.count--;
								} else {
									this.show = true;
									clearInterval(this.timer);
									this.timer = null;
								}
							}, 1000)
						}
						
					} else {
						uni.showToast({
							title: '发送失败'
						})
					}
				})
			},
			//微信授权登录 跳转页面
			toWxLogin(){
				//获取信息存缓存
				var t=this;
			
				var _member = uni.getStorageSync("member")
				if(_member!=''&&_member!=null&&_member!={}){
					uni.navigateTo({
						url: '/pages/register/Register'
					})
				}else{
					
					this.getCode();
				}
				
			},
					
			/**
			 * 根据参数名 获取 URL 路径中的参数  
			 * @param {String} name 要读取的参数名称  
			 */  
			getUrlParam(){
				  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')  
				  let url = window.location.href.split('#')[0]   
				  let search = url.split('?')[1]  
				  if (search) {  
				    var r = search.substr(0).match(reg)  
				    if (r !== null) return unescape(r[2])  
				    return null  
				  } else {  
				    return null  
				  }  
			},
			//获取code
			getCode() {
				var code = this.getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
				var local = this.$api.wxurl;  //在文件夹里index.js
				/* 静默授权：snsapi_base  snsapi_userinfo*/
				if (code == null || code === '') {
					window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.appId +
						'&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect'
						
				} else {
					/* 调用后台接口 获取token  是否登录过App的状态 */
					this.$api.getMemeberInfo({code:code}).then(res => {
										
					})
				
				}
			},
			//实时获取输入的验证码
			inputCode: function(event) {
				this.code = event.target.value
				
			},
			//实时获取输入的手机号
			inputPhone: function(event) {
				this.phone = event.target.value
				
			},
		},
		onBackPress(options) {
		    // 这里可以自定义返回逻辑，比如下面跳转其他页面
		   if(this.canBack == false){
			   // return true 表示禁止默认返回
			   return true;
		   }else{
			   return false;
		   }
		},
	}
</script>

<style>
	.page {
		width: 100%;
		height: 100vh;
	}

	.top {
		width: 750rpx;
		height: 848rpx;
	}

	.user {
		width: 162rpx;
		height: 162rpx;
		margin-left: 294rpx;
		margin-top: 242rpx;
	}

	.login {
		display: block;
		margin-left: 339rpx;
		margin-top: 45rpx;
	}

	.tel {
		width: 630rpx;
		height: 109rpx;
		line-height: 109rpx;
		margin-left: 60rpx;
		margin-top: 45rpx;
		border-bottom: 1rpx solid #999;
	}

	.tel input {
		padding-left: 30rpx;
		width: 100%;
		height: 100%;
	}

	.checked {
		position: relative;
		width: 630rpx;
		height: 108rpx;
		margin-left: 60rpx;
		border-bottom: 1rpx solid #999;
	}

	.checked input {
		padding-left: 30rpx;
		width: 100%;
		height: 100%;
	}

	.checked text {
		position: absolute;
		right: 0;
		top: 45rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		text-decoration: underline;
		color: rgba(254, 177, 71, 1);
	}

	.login-now {
		position: relative;
		z-index: 100;
		width: 386rpx;
		height: 76rpx;
		line-height: 76rpx;
		margin-left: 182rpx;
		margin-top: -20rpx;
	}

	.login-now image {
		width: 386rpx;
		height: 76rpx;
	}

	.login-now text {
		position: absolute;
		left: 126rpx;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
	}

	.bottom-img {
		width: 750rpx;
		height: 776rpx;
		margin-top: -150rpx;
	}

	.bottom-img image {
		width: 750rpx;
		height: 776rpx;
	}

	.register {
		position: relative;
		z-index: 100;
		display: block;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		text-decoration: underline;
		color: rgba(102, 102, 102, 1);
		margin-left: 323rpx;
		margin-top: 63rpx;
	}
</style>
